package org.example.chat.server;

import org.example.chat.common.Message;
import org.example.chat.common.MessageType;
import org.example.chat.common.User;

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author : hmxchen
 * @program : modules-api
 * @description :
 * @create : 2022-04-26 12:01
 **/
public class QQServer {

    public QQServer() throws Exception {
    }

    public static void main(String[] args) throws Exception {
        new QQServer().server();
    }

    private ServerSocket serverSocket = new ServerSocket(8888);

    static Map<String, User> loginUsers = new ConcurrentHashMap<>();

    static {
        loginUsers.put("100",new User("100","123"));
        loginUsers.put("200",new User("200","123"));
        loginUsers.put("300",new User("300","123"));
    }

    ManageServerThreadConnectClient manageServerThreadConnectClient = new ManageServerThreadConnectClient();

    public void server() throws Exception {
        while (true){
            //等待客户端连接
            Socket socket = serverSocket.accept();
            System.out.println("等待客户端连接成功啦");
            //客户端连接上肯定会发送消息，就会由服务器中转，所以服务器需要 输入输出
            System.out.println("开始收听客户端发送过来的消息");
            InputStream inputStream = socket.getInputStream();
            ObjectInputStream ois = new ObjectInputStream(inputStream);
            //知道客户端连接上就会求着服务器给检查用户登录的活，所以用将消息转换为User类型
            User user = (User) ois.readObject();
            System.out.println("开始判断登陆是否成功");
            boolean isLogin = checkLoginUser(user);
            if (isLogin){
                System.out.println(user.getUserId()+": 登陆成功");
                //到这服务端就把客户端交给的 检查用户登录的问题解决了，接下来就是和客户端单独再建一个线程服务客户啦
                ServerThreadConnectClient serverThreadConnectClient = new ServerThreadConnectClient(socket,user.getUserId());
                new Thread(serverThreadConnectClient).start();
                //既然是登录用户，那么就把这个登录用户 专门记录下，存到在线用户的集合里
                manageServerThreadConnectClient.addServerThreadConnectClient(user.getUserId(),serverThreadConnectClient);

                //如果服务器检查出来 客户端发送过来的人 是登录用户，就将消息发送给客户端，好好对人家
                Message message = new Message();
                message.setSender(user.getUserId());
                message.setMsgType(MessageType.LOGIN_SUCCESS.getCode());
                //将消息赶紧发送给客户端，服务好客人'
                ServerThreadConnectClient serverThread = manageServerThreadConnectClient.queryServerThreadConnectClient(user.getUserId());
                OutputStream outputStream = serverThread.getSocket().getOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(outputStream);
                oos.writeObject(message);

                String allUserIds = manageServerThreadConnectClient.onlineUserss();
                System.out.println("allUserIds: "+allUserIds);
            }else {
                System.out.println(user.getUserId() +"：登陆失败");
                //如果服务器查出来此人没有登录，也要告诉客户端
                Message message = new Message();
                message.setSender(user.getUserId());
                message.setMsgType(MessageType.LOGIN_FAILED.getCode());
                //将消息赶紧发送给客户端，服务好客人
                OutputStream outputStream = socket.getOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(outputStream);
                oos.writeObject(message);
                //告诉客户端后，客户也没登陆，就退出吧，没人需要服务啦
                serverSocket.close();
            }
        }
    }

    private boolean checkLoginUser(User user) {
        User u = loginUsers.get(user.getUserId());
        if (u == null){
            return false;
        }
        if (!u.getPassword().equals(user.getPassword())){
            return false;
        }
        return true;
    }
}
